package org.example;
import java.util.Arrays;
import java.util.List;
public class Test {
    public static void main(String[] args) {
        int[]array = new int[]{1,9,2,8,3,7,4,6,5};
        //bubbleSort(array);
        //selectSort(array);
        //insertSort(array);
        //shellSort(array);
        //heapSort(array);
        //quickSort(array);
        mergeSort(array);
        System.out.println(Arrays.toString(array));
    }

    private static void mergeSort(int[] array) {
        merge(array,0,array.length-1);
    }

    private static void merge(int[] array, int start, int end) {
        if(start>=end){
            return;
        }
        int mid = (start+end)/2;
        merge(array,start,mid);
        merge(array,mid+1,end);
        merge1(array,start,mid,end);
    }

    private static void merge1(int[] array, int start, int mid, int end) {
        int s1 = start;
        int s2 = mid+1;
        int[]tmp = new int[end-start+1];
        int k = 0;
        while(s1<=mid&&s2<=end){
            if(array[s1]<=array[s2]){
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        while(s1<=mid){
            tmp[k++] = array[s1++];
        }
        while (s2<=end){
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < tmp.length; i++) {
            array[i+start] = tmp[i];
        }
    }

    private static void quickSort(int[] array) {
       quick(array,0,array.length-1);
    }

    private static void quick(int[] array, int start, int end) {
        if(start>=end){
            return;
        }
        int pivot = partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }

    private static int partition(int[] array, int left, int right) {
        int key = array[left];
        int i = left;
        while (left<right){
            while(left<right&&array[right]>=key){
                right--;
            }
            while(left<right&&array[left]<=key){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,i,left);
        return left;
    }

    private static void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length-1;
        while(end>0){
            swap(array,0,end);
            siftDown(array,0,end);
            end--;
        }
    }

    private static void siftDown(int[] array, int parent, int end) {
        int child = 2*parent+1;
        while (child<end){
            if(child+1<end&&array[child]<array[child+1]){
                child++;
            }
            if(array[child]>array[parent]){
                swap(array,child,parent);
                parent = child;
                child = 2*parent+1;
            }else {
                break;
            }
        }
    }

    private static void createBigHeap(int[] array) {
        int n = array.length;
        for (int i = (n-1-1)/2; i >=0 ; i--) {
            siftDown(array,i,n);
        }
    }

    private static void shellSort(int[] array) {
        int gap = array.length;
        while(gap>1){
            gap/=2;
            insertSort1(array,gap);
        }
    }

    private static void insertSort1(int[] array, int gap) {
        int n = array.length;
        for (int i = gap; i < n; i++) {
            int tmp = array[i];
            int j = i-gap;
            for ( ; j >=0; j -=gap) {
                if(array[j]>tmp){
                    array[j+gap] = array[j];
                }else{
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    private static void insertSort(int[] array) {
        int n = array.length;
        for (int i = 1; i < n; i++) {
            int tmp = array[i];
            int j = i-1;
            for ( ; j >=0; j --) {
                if(array[j]>tmp){
                    array[j+1] = array[j];
                }else{
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    private static void selectSort(int[] array) {
        int n = array.length;
        for (int i = 0; i < n ; i++) {
            int minIndex = i;
            for (int j = i+1; j < n ; j++) {
                if(array[j]<array[minIndex]){
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }

    private static void bubbleSort(int[] array) {
        int n = array.length;
        for (int i = 0; i < n-1 ; i++) {
            for (int j = 0; j < n-1-i; j++) {
                if(array[j]>array[j+1]){
                    swap(array,j,j+1);
                }
            }
        }
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
}
